package me.zk.practice1.hw;

import java.util.Scanner;

/**
 * @author zhangkai
 * @date 2023/8/9 11:19
 */
public class HWBJ45 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int len = Integer.parseInt(scanner.nextLine());

        String text = scanner.nextLine();

        String[][] ms = new String[len][2];

        for (int i = 0; i < len; i++) {
            String s = scanner.nextLine();
            String[] ss = s.split(" ");
            ms[i][0] = ss[0];
            ms[i][1] = ss[1];
        }
        int index = 0;

        for (int i = 0; i < len; i++) {
            switch (ms[i][0]){
                case "FORWARD" :
                    int a = Integer.parseInt(ms[i][1]);
                    index = Math.min(a+index,text.length());
                    break;
                case "BACKWARD" :
                    int b = Integer.parseInt(ms[i][1]);
                    index = Math.max(index-b,0);
                    break;
                case "SEARCH-FORWARD" :
                    index = searchForWard(text,index,ms[i][1]);
                    break;
                case "SEARCH-BACKWARD" :
                    index = searchBackWard(text,index,ms[i][1]);
                    break;
                case "REPLACE" :
                    int l = ms[i][1].length();
                    if (index+l > text.length()) {
                        text = text.substring(0,index)+ms[i][1];
                    } else {
                        text = text.substring(0,index)+ms[i][1] + text.substring(index+l);
                    }
                    index+=l;
                    break;
                case "DELETE" :
                    int de = Integer.parseInt(ms[i][1]);
                    if (de + index > text.length()) {
                        text = text.substring(0,index);
                    } else {
                        text = text.substring(0,index) + text.substring(index+de);
                    }
                    break;
                case "INSERT" :
                    int tl = ms[i][1].length();
                    text = text.substring(0,index) + ms[i][1] + text.substring(index);
                    index+=tl;
                    break;
            }

        }
        System.out.println(text);

    }

    public static void main1(String[] args) {
        System.out.println(searchBackWard("hello",2,"el"));

    }
    public static int searchForWard(String text,int index,String target) {
       int len = text.length();
        for (int i = index; i <= len-target.length(); i++) {
            String sub = text.substring(i,i+target.length());
            if (target.equals(sub)){
                return i;
            }
        }

        return index;
    }

    public static int searchBackWard(String text,int index,String target) {
        for (int i = index; i >= target.length(); i--) {
            String sub = text.substring(i-target.length(),i);
            if (target.equals(sub)){
                return i-target.length();
            }
        }

        return index;
    }
}
